package GUI;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import javax.swing.JButton;

import MessagePacks.GetAllPack;
import MessagePacks.MessagePack;
import MessagePacks.NewTimeRequest;
import MessagePacks.UpdatePack;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class TimeExtensionRequest extends MasterGUI  {
	
	private JTextField days;
	private static final long serialVersionUID = 1L;
	private MainGUI mainGui;
	private JButton btnSend;
	private UpdatePack upExecflag;
	private NewTimeRequest newTmRqt;
	private JTextArea textArea;
	private UpdatePack activeStat;
	private GetAllPack daysLeft;
	
	public TimeExtensionRequest(MainGUI mainGui){
		super(mainGui);
		this.mainGui=mainGui;
		
		getdaysleft();
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 884, 40);
		getContentPane().add(panel);
		
		JLabel lblTimeExtensionRequest = new JLabel("Time Extension Request");
		lblTimeExtensionRequest.setFont(new Font("Dialog", Font.BOLD, 16));
		panel.add(lblTimeExtensionRequest);
		
		JLabel lblNumberOfDays = new JLabel("Number Of Days");
		lblNumberOfDays.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNumberOfDays.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblNumberOfDays.setBounds(141, 82, 124, 23);
		getContentPane().add(lblNumberOfDays);
		
		days = new JTextField();
		days.setBounds(275, 85, 198, 20);
		getContentPane().add(days);
		days.setColumns(10);
		
		JLabel lblRequestDetails = new JLabel("Request Details");
		lblRequestDetails.setHorizontalAlignment(SwingConstants.RIGHT);
		lblRequestDetails.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblRequestDetails.setBounds(141, 145, 124, 23);
		getContentPane().add(lblRequestDetails);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(275, 152, 198, 118);
		getContentPane().add(scrollPane);
		
		textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		
		setBtnSend();
			
	}
	
	public JButton getBtnSend() {
		return btnSend;
	}


	public void setBtnSend() {
		btnSend = new JButton("Send");
		if(daysLeft.getNumber()>3){
			btnSend.setEnabled(false);
			JOptionPane.showMessageDialog(serverGUI.mainframe,"You can not send the request at this time.");
		}
		else{
			btnSend.setEnabled(true);
		}
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updtExecFlag();
				createNewTimeRequest();
				UpdateStatusToWait();
				btnSend.setEnabled(false);
				JOptionPane.showMessageDialog(serverGUI.mainframe,"Your request was sent.");			
			}


		});
		btnSend.setBounds(328, 310, 89, 23);
		getContentPane().add(btnSend);
	}
	
	private void updtExecFlag(){
		upExecflag = new UpdatePack(MessagePack.updateExecTimeFlag);
		upExecflag.setProcId(mainGui.process.getprocessID());
		upExecflag.setResponOk(1);
		mainGui.client.handleMessageFromClientUI(upExecflag);
	}
	
	private void createNewTimeRequest() {
		newTmRqt = new NewTimeRequest();
		newTmRqt.setProcID(mainGui.process.getprocessID());
		newTmRqt.setPhase(mainGui.process.getCurrentPhase());
		newTmRqt.setNumOfDays(Integer.parseInt(days.getText()));
		newTmRqt.setMsg(textArea.getText());
		mainGui.client.handleMessageFromClientUI(newTmRqt);
	}
	
	private void UpdateStatusToWait() {
		activeStat = new UpdatePack(MessagePack.updateStatus);
		activeStat.setStatus("Waiting For Time Approval");
		activeStat.setProcId(mainGui.process.getprocessID());
		mainGui.client.handleMessageFromClientUI(activeStat);
	}
	
	private void getdaysleft(){
		daysLeft = new GetAllPack(MessagePack.gettimeleft);
		daysLeft.setProcID(mainGui.process.getprocessID());
		mainGui.client.handleMessageFromClientUI(daysLeft);
		daysLeft = (GetAllPack) mainGui.client.getMessage();
	}
	
	
}
